package main

import "reflect"

func walk(x interface{}, f func(input string)) {
	val := getValue(x)

	numberOfValues := 0
	var getFiled func(int) reflect.Value

	switch val.Kind() {
	case reflect.String:
		f(val.String())
	case reflect.Slice, reflect.Array:
		numberOfValues = val.Len()
		getFiled = val.Index
	case reflect.Struct:
		numberOfValues = val.NumField()
		getFiled = val.Field
	}

	for i := 0; i < numberOfValues; i++ {
		walk(getFiled(i).Interface(), f)
	}
}

func getValue(x interface{}) reflect.Value {
	val := reflect.ValueOf(x)

	//指针类型不能时候用 NumFiled ,需要使用Elem()提取底层值
	if val.Kind() == reflect.Ptr {
		val = val.Elem()
	}
	return val
}
